<template>

  <div style="width: 90%;margin: 0 auto">
    <div class="style-top">
      <span>创建班级</span>
    </div>
    <el-card>
      <el-row :gutter="15">
        <el-form ref="addClass" :model="classData" :rules="rules" size="medium" label-width="100px"
                 label-position="top">
          <el-col :span="12">
            <el-row :gutter="15">
              <el-col :span="24">
                <el-form-item label="所属校区" prop="schoolId">
                  <el-select v-model="schoolId" placeholder="请选择所属校区" clearable
                             :style="{width: '100%'}">
                    <el-option v-for="(item, index) in schoolIdOptions" :key="index" :label="item.label"
                               :value="item.value"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="班级编号" prop="classNumber">
                  <el-input v-model="classData.classNumber" placeholder="请输入班级编号" clearable
                            :style="{width: '100%'}"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="班级名称" prop="className">
                  <el-input v-model="classData.className" placeholder="请输入班级名称" clearable
                            :style="{width: '100%'}"></el-input>
                </el-form-item>
              </el-col>

              <el-col :span="24">
                <el-form-item label="备注信息" prop="classNote">
                  <el-input v-model="classData.classNote" type="textarea" placeholder="请输入备注信息"
                            :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="12">
            <el-row :gutter="15">
              <el-col :span="24">
                <el-form-item label="所授课程" prop="courseId">
                  <el-select v-model="classData.courseId" placeholder="请选择所授课程" clearable
                             :style="{width: '100%'}">
                    <el-option v-for="(item, index) in courseIdOptions" :key="index" :label="item.label"
                               :value="item.value"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="24">
                <el-form-item label="任课老师" prop="teacherId">
                  <el-select v-model="classData.teacherId" placeholder="请选择课老师" clearable
                             :style="{width: '100%'}">
                    <el-option v-for="(item, index) in teacherIdOptions" :key="index" :label="item.label"
                               :value="item.value"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>


              <el-col :span="24">
                <el-form-item label="默认扣除课时" prop="classDeductHours">
                  <el-input v-model="classData.classDeductHours" placeholder="请输入默认扣除课时" clearable
                            :style="{width: '100%'}"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="上课时间" prop="classTime">
                  <el-input v-model="classData.classTime" placeholder="请输入上课时间" clearable
                            :style="{width: '100%'}"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="24">
            <el-form-item size="large">
              <el-button type="primary" @click="submitForm">提交</el-button>
              <el-button @click="resetForm">重置</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
    </el-card>
  </div>
</template>
<script>
import {getSchoolAll, getSchoolCourse} from "../../../api/school/school";
import {parseTime} from "../../../utils/ruoyi";
import {addClass} from "../../../api/school/class";

export default {
  components: {},
  props: [],
  data() {
    return {
      classData: {
        schoolId: null,
        classNumber: null,
        teacherId: null,
        classNote: null,
        courseId: null,
        className: null,
        classDeductHours: 1,
        classTime: null,
      },
      rules: {
        schoolId: [{
          required: true,
          message: '请选择所属校区',
          trigger: 'change'
        }],
        classNumber: [{
          required: true,
          message: '请输入班级编号',
          trigger: 'blur'
        }],
        teacherId: [{
          required: true,
          message: '请选择课老师',
          trigger: 'change'
        }],

        courseId: [{
          required: true,
          message: '请选择所授课程',
          trigger: 'change'
        }],
        className: [{
          required: true,
          message: '请输入班级名称',
          trigger: 'blur'
        }],
        classDeductHours: [{
          required: true,
          message: '请输入默认扣除课时',
          trigger: 'blur'
        }],
        classTime: [{
          required: true,
          message: '请输入上课时间',
          trigger: 'blur'
        }],
      },
      schoolId: null,
      schoolIdOptions: [],
      teacherIdOptions: [],
      courseIdOptions: [],
    }
  },
  computed: {},
  watch: {
    schoolId(schoolId) {
      this.teacherIdOptions = [];
      this.courseIdOptions = [];
      this.classData.teacherId=null
      this.classData.courseId=null
      this.classData.schoolId = schoolId;
      this.getTeacherAndCourse();
    }
  },
  created() {
    this.getSchoolAllData();
    this.classNumber();



  },
  mounted() {

  },
  methods: {
    submitForm() {
      this.$refs['addClass'].validate(valid => {
        console.log(this.classData);
        addClass(this.classData).then(res=>{
          console.log(res);
          if (res.code === 200) {
            this.msgSuccess("添加班级成功")
          }
        })
      })
    },
    resetForm() {
      this.$refs['addClass'].resetFields()
    },
    classNumber(){
      let time = parseTime(new Date(),'{y}{m}{d}{h}{i}{s}');
      this.classData.classNumber="CN"+time;
    },
    /**
     * 获取校区数据
     */
    getSchoolAllData() {
      getSchoolAll().then(res => {
        if (res.code === 200) {
          const data = []
          res.data.forEach(item => {
            data.push({
              label: item.schoolName,
              value: item.schoolId
            })
          })
          this.schoolIdOptions = data;
          this.schoolId = data[0].value;
        }
      })
    },
    /**
     * 获取老师和课程数据
     */
    getTeacherAndCourse() {
      getSchoolCourse(this.schoolId).then(res => {
        if (res.code === 200) {
          const teacher = [];
          const course = [];
          res.data.schoolCourse.forEach(item => {
            course.push({
              label: item.courseName,
              value: item.courseId
            })
          })
          this.courseIdOptions = course;
          if (this.courseIdOptions.length > 0) {
            this.classData.courseId = this.courseIdOptions[0].value
          }
          res.data.schoolTeacher.forEach(item => {
            teacher.push({
              label: item.nickName + ` (${item.phonenumber})`,
              value: item.userId
            })
          })
          this.teacherIdOptions = teacher;
          if (this.teacherIdOptions.length > 0) {
            this.classData.teacherId = this.teacherIdOptions[0].value
          }
        }
      })
    }

  }
}

</script>
<style>
.style-top {
  color: #1e1e1e;
  height: 40px;
  line-height: 40px;
  font-size: 18px;
}
</style>
